﻿using MangoMes.Commont.DapperHelps;
using MangoMes.Commont.Domain.Products;
using MangoMes.Commont.DTOs;
using MangoMes.Commont.Infrastructures.Interfaces;
using MediatR;
using MySqlX.XDevAPI.Common;

namespace MangoMes.Write.API.Applications.ProductManaged
{
    /// <summary>
    /// 物料产品管理 列表
    /// </summary>
    public class ProductManageSeachHandler : IRequestHandler<ProductManageSeachCommon, PageDTO<ProductManage>>
    {
        #region 仓储
        /// <summary>
        /// 仓储
        /// </summary>
        private readonly IBateRepository<ProductTypes> ProductTypesRepository;
        private readonly IBateRepository<ProductManage> ProductManageRepository;
        private readonly ILogger<ProductManageSeachHandler> logger;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="productTypesRepository">产品分类</param>
        /// <param name="ProductManageRepository">产品管理</param>
        /// <param name="logger">日志</param>
        public ProductManageSeachHandler(IBateRepository<ProductTypes> productTypesRepository, IBateRepository<ProductManage> ProductManageRepository, ILogger<ProductManageSeachHandler> logger)
        {
            ProductTypesRepository = productTypesRepository;
            this.ProductManageRepository = ProductManageRepository;
            this.logger = logger;
        }
        #endregion

        #region 物料产品管理 列表 
        /// <summary>
        /// 物料产品管理 列表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<PageDTO<ProductManage>> Handle(ProductManageSeachCommon request, CancellationToken cancellationToken)
        {
            PageDTO<ProductManage> result = new PageDTO<ProductManage>();
            //异常处理
            try
            {
                //日志
                logger.LogInformation("物料产品管理列表显示成功!");

                ProductManage prodm = new ProductManage();

                var pageNum = (request.pageIndex - 1) * request.pageSize;

                //连接mySql
                string conn = "server=112.124.69.237;port=3306;uid=root;pwd=No6123666;database=MangoMesDB";

                var obj = "select * from productmanage a join producttypes b on a.productTypeId = b.productTypeId";
                var res = ProductManageRepository.GetAll(obj, prodm, conn).ToList();

                if (!string.IsNullOrEmpty(request.productManageNode))
                {
                    res = res.Where(x => x.ProductManageNode.Contains(request.productManageNode)).ToList();
                }
                if (!string.IsNullOrEmpty(request.productManageName))
                {
                    res = res.Where(x => x.ProductManageName.Contains(request.productManageName)).ToList();
                }
                result.TotalCount = res.Count();
                result.PageCount = (int)Math.Ceiling((res.Count() * 1.0) / request.pageSize);
                //sql语句
                string str = $"select * from productmanage a join producttypes b on a.productTypeId = b.productTypeId order by a.productTypeId limit " + request.pageSize + " offset " + pageNum + "";

                var ree = ProductManageRepository.GetAll(str, prodm, conn).ToList();
                
                result.Data = ree;
            }
            catch (Exception ex)
            {
                logger.LogError("物料产品管理列表显示失败！" + ex.Message);
            }
            return result;
        }
        #endregion

    }
}
